const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  // 跳转到新增地址
  toAddAddress: function (e) {
    // wx.navigateTo({
    //   url: '../add-address/add-address'
    // })
    wx.redirectTo({
      url: '../add-address/add-address?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&flag=' + this.data.flag + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName + '&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress+'&type='+this.data.type
    })
  }, 
  editAddAddress: function (e) {
    wx.redirectTo({
      url: '../add-address/add-address?id=' + e.currentTarget.id + '&startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&flag=' + this.data.flag + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName + '&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress + '&type=' + this.data.type
    })
  },
  //选择地址返回
  selectAddress: function (e) {
    if(!this.data.productId){
      return;
    }
    if (this.data.flag == 'start') {
      this.setData({
        sendType: 1,
        sendName: e.currentTarget.dataset.value.receiveName,
        sendMobile: e.currentTarget.dataset.value.receiveMobile,
        sendAddress: e.currentTarget.dataset.value.receiveFullAddress
      })
      wx.redirectTo({
        url: '../order/order?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName + '&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress + '&type=' + this.data.type
      })
    }
    if (this.data.flag == 'end') {
      this.setData({
        receiveType: 1,
        receiveName: e.currentTarget.dataset.value.receiveName,
        receiveMobile: e.currentTarget.dataset.value.receiveMobile,
        receiveAddress: e.currentTarget.dataset.value.receiveFullAddress
      })
      wx.redirectTo({
        url: '../order/order?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + this.data.startCityId + '&endCityId=' + this.data.endCityId + '&startCityName=' + this.data.startCityName + '&endCityName=' + this.data.endCityName + '&sendType=' + this.data.sendType + '&sendName=' + this.data.sendName + '&sendMobile=' + this.data.sendMobile + '&sendAddress=' + this.data.sendAddress + '&receiveType=' + this.data.receiveType + '&receiveName=' + this.data.receiveName + '&receiveMobile=' + this.data.receiveMobile + '&receiveAddress=' + this.data.receiveAddress + '&type=' + this.data.type
      })  
    }

  },

  setDefault: function (e) {
    let addresId = e.currentTarget.id
    let that = this
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/address/setDefault',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "addressId": addresId,
        "memberId": app.globalData.userInfo.id
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.loadAddress()
        }
      }
    })

  },
  deleteAddAddress: function (e) {
    let addresId = e.currentTarget.id
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否删除',
      success: function (res) {
        if (res.confirm) {

          wx.request({
            url: 'https://small.qiongyazuche.com/sunday/small/address/delete',
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              "addressId": addresId
            },
            success: function (res) {
                that.loadAddress()
            }
          })

        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productId: options.productId ? options.productId : '',
      startTime: options.startTime ? options.startTime : '',
      startHour: options.startHour ? options.startHour : '',
      endTime: options.endTime ? options.endTime : '',
      endHour: options.endHour ? options.endHour : '',
      day: options.day ? options.day : '',
      startCityId: options.startCityId ? options.startCityId : '',
      startCityName: options.startCityName ? options.startCityName : '',
      endCityId: options.endCityId ? options.endCityId : '',
      endCityName: options.endCityName ? options.endCityName : '',
      sendType: options.sendType ? options.sendType : '',
      sendName: options.sendName ? options.sendName : '',
      sendMobile: options.sendMobile ? options.sendMobile : '',
      sendAddress: options.sendAddress ? options.sendAddress : '',
      receiveType: options.receiveType ? options.receiveType : '',
      receiveName: options.receiveName ? options.receiveName : '',
      receiveMobile: options.receiveMobile ? options.receiveMobile : '',
      receiveAddress: options.receiveAddress ? options.receiveAddress : '',
      flag: options.flag ? options.flag : '',
      type: options.type ? options.type : ''
    })

    this.loadAddress()
    console.log('productId='+this.data.productId)
    console.log('startTime=' + this.data.startTime)
    console.log('startHour=' + this.data.startHour)
    console.log('endTime=' + this.data.endTime)
    console.log('endHour=' + this.data.endHour)
    console.log('day=' + this.data.day)
    console.log('startCityId=' + this.data.startCityId)
    console.log('startCityName=' + this.data.startCityName)
    console.log('endCityId=' + this.data.endCityId)
    console.log('endCityName=' + this.data.endCityName)
    console.log('sendType=' + this.data.sendType)
    console.log('sendName=' + this.data.sendName)
    console.log('sendMobile=' + this.data.sendMobile)
    console.log('sendAddress=' + this.data.sendAddress)
    console.log('receiveType=' + this.data.receiveType)
    console.log('receiveName=' + this.data.receiveName)
    console.log('receiveMobile=' + this.data.receiveMobile)
    console.log('receiveAddress=' + this.data.receiveAddress)
    console.log("flag="+this.data.flag)
    console.log("type="+this.data.type)
      },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  },
  loadAddress:function(){
    let that = this
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/address/select',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "memberId": app.globalData.userInfo.id
      },
      success: function (res) {

        that.setData({
          addresses: res.data.result
        })

      }
    })
  }
})